<?php
include "otamu_config.php";

$OBJECT = 'sub_item';
$OBJECT_TABLE = $OBJECT.'s';

$layout = "layout";
define("_VIEWPATH_","views/$OBJECT/");

if (!isset($_GET['act'])) $_GET['act'] = ''; 

class Sub_item extends ADOdb_Active_Record
{
	var $_table = "sub_items";
}
//ADOdb_Active_Record::ClassBelongsTo('Item','item_group','item_group_id','id'); 
//ADOdb_Active_Record::ClassBelongsTo('Item','graph_type','graph_type_id','id');

switch ($_GET['act']) {
	case "new" :
		break;
	case "select_group" :
		break;
	case "decide_single" :
		break;

	case "create" :
		if ($_REQUEST[$OBJECT]['name']=='') exitError("Sila Masukkan Nama ". ucwords($OBJECT));
		//check if sub item already exist
		$name    = $_REQUEST[$OBJECT]['name'];
		$item_id = $_REQUEST[$OBJECT]['item_id'];
		$sql = "SELECT name FROM $OBJECT_TABLE WHERE name ilike '$name' AND item_id='$item_id'";
		$n = pg_num_rows(pg_query($sql));
		if ($n>0) exitError($OBJECT . " already exists.");
				
		$new_object = ucwords($OBJECT);
		$obj = new $new_object();
		//$obj = new SubItem();
		
		foreach ($_REQUEST[$OBJECT] as $k=>$v) {
			$obj->$k = $v;
		}
		if ($_REQUEST['tarikh_tubuh']=='') {
			$obj->tarikh_tubuh = 'null';
		}
		
		if ($_REQUEST['external'] == '1') {
			$obj->obj_category_id = $_REQUEST['external_table_id'];
			$obj->obj_id = $_REQUEST['external_obj_id'];
		}
		print_r($obj);exit;

		if (!$obj->save()) { 
			echo $obj->ErrorMsg();exit;
			$_SESSION['flash'] = $obj->ErrorMsg();
		}else {
			$_SESSION['flash'] = ucwords($OBJECT).' has been added';
		}
		header("Location: " .$OBJECT. ".php?act=list");
		break;
		
	case "edit" :
		if (!isset($id) || $id=='') {
			exitError(ucwords($OBJECT) . " not found.");
		}
		$new_object = ucwords($OBJECT);
		$sub_item = new $new_object();
		//$item = new SubItem();
		$sub_item->Load('id='.$id);
		$name = $sub_item->name;
		$unit = $sub_item->unit;
		
		if ($sub_item->obj_category_id == '') {
			$external_checked_yes = '';
			$external_checked_no  = 'checked';
		}
		else {
			//$external_object_name = getNameFromId('object_categories',$sub_item->obj_category_id);
			$external_checked_yes = 'checked';
			$external_checked_no  = '';
			$external_table_id = $sub_item->obj_category_id;
		}
		
		//get group id
		$sql = "SELECT s.id,i.id as item_id,g.id as group_id FROM sub_items s 
		         INNER JOIN items i ON i.id=s.item_id 
				 INNER JOIN item_groups g ON g.id=i.item_group_id 
				 WHERE s.id='$id';";
		$res = pg_query($sql);
		$dat = pg_fetch_object($res,0);
		$group_id = $dat->group_id;

		break;

	case "update" :
		if (!isset($id) || $id=='') {
			exitError(ucwords($OBJECT) . " not found.");
		}

		if ($_REQUEST[$OBJECT]['name']=='') exitError("Sila Masukkan Nama ". ucwords($OBJECT));
		//check if sub item already exist
		$name    = $_REQUEST[$OBJECT]['name'];
		$item_id = $_REQUEST[$OBJECT]['item_id'];
		$sql = "SELECT name FROM $OBJECT_TABLE WHERE name ilike '$name' AND item_id='$item_id' AND id!='$id'";
		$n = pg_num_rows(pg_query($sql));
		if ($n>0) exitError($OBJECT . " already exists.");

		$new_object = ucwords($OBJECT);
		$obj = new $new_object();

		$obj->Load("id='$_REQUEST[id]'");
		//print_r($obj);

		foreach ($_REQUEST[$OBJECT] as $k=>$v) {
			$obj->$k = $v;
		}
		//print_r($obj);exit;

		if (!$obj->save()) { 
			$_SESSION['flash'] = $obj->ErrorMsg();
		}else {
			$_SESSION['flash'] = ucwords($OBJECT) . ' has been updated';
		}
		header("Location: $OBJECT.php?act=list");
		
		break;

	case "destroy" :
		if (isset($id)) {
			/*$check = "SELECT DISTINCT(item_group_id) FROM items WHERE item_group_id='$id'";
			$n = pg_num_rows(pg_query($check));
			if ($n>0) {
				exitError("This group is still in use and cannot be deleted.");
			}*/
			$sql = "DELETE FROM $OBJECT_TABLE WHERE id='$id'";
			$res = pg_query($sql);
			$_SESSION['flash'] = ucwords($OBJECT) . ' has been deleted';
			header("Location: $OBJECT.php?act=list");
		}
		else {
			echo "group not found $btn";exit;
		}
		break;
	
	default :
		$act = "list";
		$res = pg_query("SELECT si.*,i.name as item_name,gt.name as graph_type_name FROM sub_items si
		                 LEFT JOIN items i ON i.id=si.item_id
						 LEFT JOIN graph_types gt ON gt.id=si.graph_type_id
						 ORDER BY item_name,si.name");
		
}

include _VIEWPATH_."$layout.php";

?>
